
---@class CustomPatrolHandler:ActionHandlerBase
CustomPatrolHandler = class(ActionHandlerBase)

function CustomPatrolHandler:Enter()
    self.base.Enter(self);
    Debug.Log("进入巡逻动作");

    ---@type UnityEngine.Transform
    self._self = self:GetGameData(ECustomDataName.SelfTrans);
    ---@type UnityEngine.CharacterController
    self._controller = self._self:GetComponent(typeof(CS.UnityEngine.CharacterController));

    ---随机查找目标巡逻位置
    ---@type UnityEngine.Vector3
    local originalPos = self:GetGameData(ECustomDataName.OriginalPos);
    local targetPos = CS.UnityEngine.Vector3(0,0,0);
    targetPos.x = originalPos.x + math.random(0,10) - 5;
    targetPos.y = originalPos.y;
    targetPos.z = originalPos.z + math.random(0,10) - 5;
    self:SetGameData(ECustomDataName.TargetPos,targetPos);
end

function CustomPatrolHandler:Execute()
    self.base.Execute(self);
    local targetPos = self:GetGameData(ECustomDataName.TargetPos)
    if CS.UnityEngine.Vector3.Distance(self._self.position, targetPos) > 0.5 then
        local dirToEnemy = (targetPos - self._self.position).normalized;
        self._controller:SimpleMove(dirToEnemy * 4);
    else
        self:OnComplete();
    end
end
